package ellements;

import ellements.drawable.Desk;
import sounds.MyMedia;
import matrix.world.volleyball.MySurfaceView;
import matrix.world.volleyball.R;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Rect;

public class Crab extends Sprite {
	protected final float Floor = 266f*MySurfaceView.rate;
	public float x=MySurfaceView.Width/4f;
	public float y=Floor;
	public float tox;
	protected int squence=1;
	protected float xold;
	
	protected float dstep = 2f*MySurfaceView.rate;
	
	protected float vy;
	protected final float Gravity=0.8f*MySurfaceView.rate*MySurfaceView.LEVEL;
	protected final float DUMCEG=-0.1f;
	protected boolean isJump = false;
	
	protected final int width = (int) (72f*MySurfaceView.rate);
	protected final int height= (int) (72f*MySurfaceView.rate);
	public final float radius = 36f*MySurfaceView.rate;
	public final float power = 10f*MySurfaceView.rate*MySurfaceView.LEVEL;
	protected int iframe;
	
	
	protected Bitmap mBitmap;
	
	public Crab(){
		
	}
	
	public Crab(Resources res){
		mBitmap = BitmapFactory.decodeResource(res, R.drawable.craba);
		
		Matrix matrix = new Matrix();
		matrix.postScale(MySurfaceView.rate, MySurfaceView.rate);
		mBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
		
		tox=x;
	}
	public void draw(Canvas c){
		Rect src; 
        Rect dst;
        oval.left = x-36*y/268;
        oval.top = 288f*MySurfaceView.rate-12*y/268;
        oval.right = x+36*y/268;
        oval.bottom = 288f*MySurfaceView.rate+12*y/268;
        c.drawOval(oval, pa);
        int xframe = width*iframe;
        src = new Rect(xframe, 0, xframe+width, height);
        dst = new Rect((int)(x-width/2), (int) (y-height/2),(int) (x+width/2), (int) (y + height/2));
        c.drawBitmap(mBitmap, src, dst, null);
	}
	
	public void animation(){
		vy+=Gravity;
		y+=vy;
		if (y>Floor){
			if (isJump) nextframe();
			isJump = false;
			y=Floor;
			vy*=DUMCEG;
		}
		x=x+(tox-x)/squence;
		if (squence>1) squence--;
		if (x<radius){
			x=radius;
		}else if (x>Desk.NetLeft-radius*0.6f) {
			x=Desk.NetLeft-radius*0.6f;
		}
		if (xold-x>dstep || xold-x<-dstep){
			xold = x;
	        nextframe();
		}
	}
	
	public void setMoveTo(float tox){
		this.tox = tox;
		squence=5;
	}
	
	public void jump(){
		if (!isJump){
			vy=-12*MySurfaceView.rate*MySurfaceView.LEVEL;
			isJump = true;
			nextframe();
		}
	}
	
	protected void nextframe(){
		 iframe++;
	     if (iframe>2) {
	    	 iframe=0;
	    	 MyMedia.playMove();
	     }
	     
	     
	}
	
}
